import Login from '../views/login'
import Home from '../views/home'
import Layout from '../views/layout'
import UsermanegeList from '../views/user-manage/list'
import RightManageRoleList from '../views/right-manage/role-list'
import RightManageRightList from '../views/right-manage/right-list'

import NewsAdd from '../views/news-manage/news-add'
import NewsCategory from '../views/news-manage/news-category'
import NewsDraft from '../views/news-manage/news-draft'

import NewsPreview from '../views/news-manage/news-preview'
import NewsUpdate from '../views/news-manage/news-update'
import Audit from '../views/audit-manage/audit'
import AuditManageList from '../views/audit-manage/list'

import Unpublished from '../views/publish-manage/unpublished'
import Published from '../views/publish-manage/published'
import Sunset from '../views/publish-manage/sunset'

import Detail from '../views/news/Detail'
import News from '../views/news/News'

import { Navigate } from 'react-router-dom'

const LocalRouterMap = {
  '/home': <Home />,
  '/user-manage/list': <UsermanegeList />,
  '/right-manage/role/list': <RightManageRoleList />,
  '/right-manage/right/list': <RightManageRightList />,
  '/news-manage/add': <NewsAdd />,
  '/news-manage/draft': <NewsDraft />,
  '/news-manage/category': <NewsCategory />,
  '/news-manage/preview/:id': <NewsPreview />,
  '/news-manage/update/:id': <NewsUpdate />,
  '/audit-manage/audit': <Audit />,
  '/audit-manage/list': <AuditManageList />,
  '/publish-manage/unpublished': <Unpublished />,
  '/publish-manage/published': <Published />,
  '/publish-manage/sunset': <Sunset />,
}

const routes = [
  { path: '/', element: <Navigate to="/home" /> },
  
  {
    path: '/',
    element: <Layout />,
    children: [
      // {
      //   path: '/home',
      //   element: <Home />,
      // },
      // {
      //   path: '/user-manage/list',
      //   element: <UsermanegeList />,
      // },
      // {
      //   path: '/right-manage/role/list',
      //   element: <RightManageRoleList />,
      // },
      // {
      //   path: '/right-manage/right/list',
      //   element: <RightManageRightList />,
      // },
    ],
  },
  { path: '/login', element: <Login /> },

  {
    path: '*',
    element: <Navigate to="/home" />,
  },
]

export default routes

export const routerFn = (BackRouteList = []) => {
  let token = JSON.parse(localStorage.getItem('xinwen-token'))

  let arr = []
  if (token) {
    const {
      role: { rights },
    } = token
    BackRouteList.forEach((item) => {
      if (rights.includes(item.key)) {
        arr.push({
          path: item.key,
          element: LocalRouterMap[item.key],
        })
      }
    })
  }
  console.log(arr)
  const routes1 = [
    { path: '/', element: <Navigate to="/home" /> },
    {
      path: '/',
      element: <Layout />,
      children: arr,
    },
    { path: '/login', element: <Login /> },
    {
      path: '*',
      element: <Navigate to="/home" />,
    },
    { path: '/news', element: <News /> },
    { path: "/detail/:id", element: <Detail /> },
  ]

  return routes1
}
